package com.lanyou.cook.entity.kettle;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Transient;

import org.apache.commons.beanutils.PropertyUtils;

@Entity
@Table(name = "R_DATABASE")
public class KettleDataSource {

	private long id;

	private String name;
	
	private static final int DEFAULT_CONTYPE = 1;	// Native (JDBC)
	
	private int databaseContypeId = DEFAULT_CONTYPE;
	
	private String hostName;
	
	private int port;
	
	private String databaseName;
	
	private String username;
	
	private String password;
	
	private KettleDatabaseType dataBaseType;

	@Id
	@Column(name="id_database")
	@GeneratedValue(strategy = GenerationType.TABLE, generator="idGenerator")  
	@TableGenerator(name = "idGenerator",  
	    table="t_cook_bu_id_generator",  
	    pkColumnName="table_name",  
	    valueColumnName="next_id",  
	    pkColumnValue="R_DATABASE",  
	    allocationSize=1  
	)
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	@Column(nullable = false, unique = true)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name="id_database_contype")
	public int getDatabaseContypeId() {
		return databaseContypeId;
	}

	public void setDatabaseContypeId(int databaseContypeId) {
		this.databaseContypeId = databaseContypeId;
	}

	@Column(name="host_name")
	public String getHostName() {
		return hostName;
	}

	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	@Column(name="database_name")
	public String getDatabaseName() {
		return databaseName;
	}

	public void setDatabaseName(String databaseName) {
		this.databaseName = databaseName;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@ManyToOne
	@JoinColumn(name = "id_database_type")
	public KettleDatabaseType getDataBaseType() {
		return dataBaseType;
	}

	public void setDataBaseType(KettleDatabaseType dataBaseType) {
		this.dataBaseType = dataBaseType;
	}
	
	@Transient
	public String getDataBaseTypeCode() {
		try {
			return (String) PropertyUtils.getProperty(dataBaseType, "code");
		} catch (Exception e) {
			return null;
		}
	}
	
	@Transient
	public String getDataBaseTypeForDisplay() {
		try {
			return (String) PropertyUtils.getProperty(dataBaseType, "description");
		} catch (Exception e) {
			return null;
		}
	}


}
